package red.lixiang.service.impl;

import red.lixiang.dao.ProductDAO;

import red.lixiang.model.*;
import red.lixiang.service.ProductService;
import red.lixiang.utils.IS_ListUtils;
import red.lixiang.utils.ProductUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.stream.Collectors;


@Repository
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDAO productDAO;


    @Override
    public BaseInfo getProductByBarCode(String barCode) {

        BaseInfo baseInfo =new BaseInfo() ;

        //对传入的barCode进行判断，如果是自定义的则不进行查数据库操作
        if(barCode.length()==10){
            Product product = ProductUtils.getProductByBarCode(barCode);
            baseInfo= new BaseInfo("0","成功获取商品信息",product);

        }else{
            Product product  = productDAO.getProductByBarCode(barCode);
            if (product != null && product.getProductNum()>0) {
                product.setSinglePrice(product.getOriginalPrice());


                List<Product> products = productDAO.getProductByProductId(product.getProductId());
                Map<String , Object> map = new HashMap<>();
                map.put("product",product);

                //从中分解颜色和尺码属性
                List<ProductAttribute> allAttr =products.stream().map(getProductAttr).collect(Collectors.toList());

                //提取出颜色的属性列表
                List<ProductAttribute> colorList =allAttr.stream().filter(IS_ListUtils.distinctByKey(ProductAttribute::getColorCode)).collect(Collectors.toList());

                //提取出第一个颜色对应的尺寸属性列表
                //List<ProductAttribute> sizeList =allAttr.stream().filter(attr->attr.getColorCode().equals(product.getColorCode())).collect(Collectors.toList());

                List<ProductAttribute> sizeList = new ArrayList<>();
                for (ProductAttribute attribute : allAttr) {
                    if (attribute.getColorCode().equals(product.getColorCode())) {
                        ProductAttribute productAttribute = new ProductAttribute();
                        productAttribute.setSizeCode(attribute.getSizeCode());
                        productAttribute.setSizeName(attribute.getSizeName());
                        sizeList.add(productAttribute);
                    }
                }


                //根据barCode设置选中
                for (ProductAttribute attribute : colorList) {
                    if (attribute.getColorCode().equals(product.getColorCode())) {
                        attribute.setIsSelect(1);
                        break;
                    }
                }
                for (ProductAttribute attribute : sizeList) {
                    if ( attribute.getSizeCode().equals(product.getSizeCode())) {
                        attribute.setIsSelect(1);
                        break;
                    }
                }

                map.put("sizeAttribute",sizeList);
                map.put("colorAttribute",colorList);

                baseInfo= new BaseInfo("0","成功获取商品信息",map);
            }else {
                if(product!=null ){
                    //给一个标识，然后重新加载
                    List<Product> products = productDAO.getProductByProductId(product.getProductId());
                    List<Product> avaProduct = products.stream().filter(product1 -> product1.getColorCode().equals(product.getColorCode())).collect(Collectors.toList());
                    Product product1 = avaProduct.get(0);
                    baseInfo = new BaseInfo("100",product1.getBarCode());
                }
            }
        }

        return  baseInfo;
    }

    @Override
    public BaseInfo getProductByProductId(String productId) {
        BaseInfo baseInfo = new BaseInfo("0","OK");
        List<Product> products = productDAO.getProductByProductId(productId);
        Map<String , Object> map = new HashMap<>();
        map.put("product",products.get(0));

        //从中分解颜色和尺码属性
        List<ProductAttribute> allAttr =products.stream().map(getProductAttr).collect(Collectors.toList());

        //提取出颜色的属性列表
        List<ProductAttribute> colorList =allAttr.stream().filter(IS_ListUtils.distinctByKey(ProductAttribute::getColorCode)).collect(Collectors.toList());

        //提取出第一个颜色对应的尺寸属性列表
        List<ProductAttribute> sizeList =allAttr.stream().filter(attr->attr.getColorCode().equals(colorList.get(0).getColorCode())).collect(Collectors.toList());

        //将第一个设置为默认选中
        colorList.get(0).setIsSelect(1);
        sizeList.get(0).setIsSelect(1);

        map.put("sizeAttribute",sizeList);
        map.put("colorAttribute",colorList);
        baseInfo.setObject(map);
        return baseInfo;
    }

    @Override
    public BaseInfo getProductImageByProductId(String productId) {
        Map<String, Object> map = new HashMap<>();
        List<ProductImageAttr> imageAttrs = productDAO.getProductImageByProductId(productId);
        List<ProductImageAttr>mainImages = imageAttrs.stream().filter(imageAttr-> imageAttr.getAttrType()==0).collect(Collectors.toList());
        List<ProductImageAttr>descImages = imageAttrs.stream().filter(imageAttr-> imageAttr.getAttrType()==1).collect(Collectors.toList());
        map.put("mainImages",mainImages);
        map.put("descImages",descImages);
        return new BaseInfo("0","OK",map);
    }

    @Override
    public BaseInfo deleteProductImageById(String id) {
        productDAO.deleteProductImageById(id);
        return new BaseInfo("0","ok");
    }

    @Override
    public BaseInfo changeProductAttribute(String productId, int selectType, String selectValue) {
        //先通过productId 获取到对应的商品
        BaseInfo baseInfo = new BaseInfo("0","OK");
        Map<String , Object> map = new HashMap<>();

        List<Product> products = productDAO.getProductByProductId(productId);

        for (Product product : products) {
            if (product.getColorCode().equals(selectValue)) {
                map.put("productImage",product.getProductImag());
                break;
            }
        }
        
        //从中分解颜色和尺码属性
        List<ProductAttribute> allAttr =products.stream().map(getProductAttr).collect(Collectors.toList());

        //提取出颜色的属性列表
        List<ProductAttribute> colorList =allAttr.stream().filter(IS_ListUtils.distinctByKey(ProductAttribute::getColorCode)).collect(Collectors.toList());

        //提取出第一个颜色对应的尺寸属性列表
        List<ProductAttribute> sizeList =allAttr.stream().filter(attr->attr.getColorCode().equals(selectValue)).collect(Collectors.toList());

        //设置选中
        for (ProductAttribute attribute : colorList) {
            if(attribute.getColorCode().equals(selectValue)){
                attribute.setIsSelect(1);
            }
        }
        sizeList.get(0).setIsSelect(1);

        map.put("sizeAttribute",sizeList);
        map.put("colorAttribute",colorList);
        baseInfo.setObject(map);

        baseInfo.setObject(map);
        return baseInfo;
    }

    @Override
    public BaseInfo getProductByPage(int currentPage, int numPerPage) {
        BaseInfo baseInfo = new BaseInfo();
        int beginNum = currentPage*numPerPage;
        List<Product> products = productDAO.getProductByPage(beginNum,numPerPage);
        baseInfo.setObject(products);
        return baseInfo;
    }

    @Override
    public BaseInfo getProductDescByProductId(String productId) {
        BaseInfo baseInfo = new BaseInfo();
        List<ProductDesc> productDescs = productDAO.getProductDescByProductId(productId);
        baseInfo.setObject(productDescs);
        return baseInfo;
    }
}
